Suppose we have an arm or a leg with a single joint, that means it has:
- 3 vertices (hip, knee, foot)
- 2 edges (thigh and calf)
- 1 joint (the knee)
Given a placement for the hip and the foot, we want to choose a natural placement for the knee.
Now let's make the somewhat obvious assumption that the thigh and calf don't change length as the joint moves.
Then that means we can draw a circle with radius equal to the thigh length. The knee must be somewhere
on the edge of this circle. We can do something similar with the calf and the foot.
That means the knee is somewhere on the edge of these two intersecting circles. Now all we have to
do is a little bit of algebra, and find out where these two circles intersect. This isn't highschool, so I'm
allowed to steal my answer from someone on the internet.
\(a = \frac{r_1^2 - r_2^2 + d^2}{2d}\)
\(h = \sqrt{r_1^2 - a^2}\)
\(x_3 = x_1 + \frac{a(x_2 - x_1)}{d}\)
\(y_3 = y_1 + \frac{a(y_2 - y_1)}{d}\)
\(x_4 = x_3 \pm \frac{h(y_2 - y_1)}{d}\)
\(y_4 = y_3 \mp \frac{h(x_2 - x_1)}{d}\)
In this demonstration, the "foot" is set to chase the mouse, and the hip is meant to stay where it is.
That just leaves us to figure out the knee using the technique above.
I think it works pretty well all things considered. Since there's usually two possible positions, we're
always taking the one on the left. This is because most joints in animals don't bend both ways very well- so
it looks weird.